// 这个文件专门用来存放关于首页的所有接口
const Router = require('koa-router');
const home = new Router();
const db = require('../utils/db');

// 写对应的接口
home.get('/', async (ctx) => {
    ctx.body = "首页home";
})
home.get('/banner', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from banner`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
                
               val.imgUrl='http://localhost:5050'+val.imgUrl;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
home.get('/bannerDetail/1', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from banner where id=1`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.imgUrl='http://localhost:5050'+val.imgUrl;
               val.img='http://localhost:5050'+val.img;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
home.get('/bannerDetail/2', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from banner where id=2`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.imgUrl='http://localhost:5050'+val.imgUrl;
               val.img='http://localhost:5050'+val.img;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
home.get('/bannerDetail/3', async (ctx) => {
    let mydata=await new Promise((resolve, reject) => {
        // 访问数据库，拿到banner这个数据给前端
        return db.query(`select * from banner where id=3`, (err, data) => {
            if (err) throw err;
            console.log(data);
            data.map(val=>{
               val.imgUrl='http://localhost:5050'+val.imgUrl;
               val.img='http://localhost:5050'+val.img;
            })
            resolve(data);
        });
    })
    ctx.body = mydata;
})
home.get('/footer', async (ctx) => {
    ctx.body = "首页--底部";
})

module.exports = home;